﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WyUnity.Data.DataAccess;

namespace DAL_13_0
{
    public static class IdentityID
    {
        public static int GetNewICBillID(DbSession session, string tablename)
        {
            var sql = @"
DECLARE @id INT ,
    @tablename VARCHAR(256)
SET @tablename = '{0}'
IF EXISTS ( SELECT  1
            FROM    ICMaxNum AS imn
            WHERE   FTableName = @tablename ) 
    BEGIN
        UPDATE  ICMaxNum
        SET     FMaxNum = FMaxNum + 1
        WHERE   FTableName = @tablename
        SELECT  FMaxNum
        FROM    ICMaxNum AS imn
        WHERE   FTableName = @tablename
    END
ELSE 
    BEGIN
        INSERT  INTO ICMaxNum
                ( FTableName, FMaxNum )
        VALUES  ( @tablename, 1 )
        SELECT  1
    END
";
            return session.QuerySclare<int>(string.Format(sql, tablename.Replace("'", "")));
        }
    }
}
